<template>
    <div style="padding: 10px;">

        <!--功能区域-->
        <div style="margin: 10px 0">

        </div>

        <!--搜索-->
        <div style="margin: 10px; text-align: center;margin-left: -120px;border-radius: 20px">
            <el-input @keyup.enter.native="load" v-model="search" placeholder="请输入关键字"
                      style="border-radius: 20px;width: 20%;" clearable=""></el-input>
            <el-button type="primary" style="margin-left: 7px" @click="load">查询</el-button>
        </div>

        <el-table
                :data="tableData"
                border
                stripe=""
                size="small"
                style="width: 100%">
            <el-table-column
                    prop="id"
                    sortable=""
                    label="ID">
            </el-table-column>
            <el-table-column
                    prop="username"
                    label="用户名">
            </el-table-column>
            <el-table-column
                    prop="nick"
                    label="昵称">
            </el-table-column>

            <el-table-column label="角色">

                <template slot-scope="scope">
                    <!--<span v-if="scope.row.role===1">管理员</span>-->
                    <!--<span v-if="scope.row.role===2">普通用户</span>-->
                    <el-tag :type="scope.row.role === 1?'success':''">
                        {{scope.row.role === 1? '管理员': '普通用户'}}
                    </el-tag>
                </template>
            </el-table-column>

            <el-table-column
                    width="170px"
                    fixed="right"
                    label="操作">
                <template slot-scope="scope">
                    <el-button title="拥有的图书列表" type="success" plain @click="showBooks(scope.row.bookList)">查看拥有的图书
                    </el-button>


                </template>
            </el-table-column>
        </el-table>


        <!--分页-->
        <div style="margin: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

            <el-dialog title="用户拥有的图书列表" :visible.sync="bookVis" width="30%">
                <el-table :data="bookList" stripe border>
                    <el-table-column prop="id" label="ID"></el-table-column>
                    <el-table-column prop="name" label="名称"></el-table-column>
                    <el-table-column prop="author" label="作者"></el-table-column>
                    <el-table-column prop="price" label="价格/元"></el-table-column>
                </el-table>
            </el-dialog>


        </div>

</template>

<script>
    // @ is an alias to /src

    import request from '../utils/request'
    import Particles from '../components/particles/index'

    export default {
        name: 'User',
        components: {Particles},
        data() {
            return {
                bookList: [],
                bookVis: false,

                form: {},
                user: {},
                //新增

                search: '',
                //当前页
                currentPage: 1,
                pageSize: 10,
                total: 0,
                roleName: "",
                tableData: []
            }


        },
        created() {
            let userStr = sessionStorage.getItem("user") || "{}"
            this.user = JSON.parse(userStr)
            this.load()

        },
        methods: {
            showBooks(books) {
                this.bookVis = true
                this.bookList = books

            },

            //查询
            load() {
                request.get('/user/selectBook', {
                    params: {
                        pageNum: this.currentPage,
                        pageSize: this.pageSize,
                        search: this.search,
                    }

                }).then(res => {

                    this.tableData = res.data.records
                    this.total = res.data.total
                })
            },


            //页码 每页的个数
            handleSizeChange(pageSize) {
                this.pageSize = pageSize
                this.load()
            },
            //改变当前页
            handleCurrentChange(pageNum) {
                this.currentPage = pageNum
                this.load()
            },

        }
    }
</script>

<style scoped>

    el-input {
        border-radius: 20px
    }
</style>
